package com.sh.Utils;


import com.sh.bean.MySample;

/**
 * 欧几里得距离计算器
 */
public class EuclideanDistanceCalculator_My implements DistanceCalculator_My {

    @Override
    public double calculate_My(MySample sample1, MySample sample2) {
        double[] data1 = sample1.getExample();  // 获取第一个样本的特征
        double[] data2 = sample2.getExample();  // 获取第二个样本的特征
        if (data1.length != data2.length) {
            throw new IllegalArgumentException("数据长度不一致");
        }
        double ret = 0;
        for (int i = 0; i < data1.length; i++) {
            ret += Math.pow(data1[i] - data2[i], 2); // 计算 平方和
        }
        return Math.sqrt(ret); // 返回平方根
    }
}
